import { message } from 'antd';
import {
  getExaminationPaper, setExamPaper, createExamColumn, setExamColumn,
  getExaminationColumn, getExaminationQuestion, setExamQuestion, deleteExamQuestion,
  deleteExamColumn, createExamQuestion, getUserExaminationPaper, setScore, getScore
} from '@/services/competition';
import moment from 'moment';

export default {
  namespace: 'paper',

  state: {
    // display: false,
    paperTitle: {},
    columnList: [],
    questionList: [],
    paper:{},
  },


  effects: {
    // *submitQuestion({ payload,callback }, { call, put}) {
    //   //yield call(fakeSubmitForm, payload);
    //   message.success('提交成功');
    //   yield put({
    //     type: 'saveStepFormData',
    //     payload : {display:true}
    //   });
    // },
    // *editQuestion({ payload,callback }, { call, put}) {
    //   //yield call(fakeSubmitForm, payload);
    //   message.success('提交成功');
    //   yield put({
    //     type: 'saveStepFormData',
    //     payload : {display:false}
    //   });
    // },
    // *getUploadAnswer({ payload, callback }, { call, put }){
    //   const response = yield call(getUploadAnswer,payload)
    //   callback&&callback(response.data)
    // },
    *getUserExaminationPaper({ payload, callback }, { call, put }){
      const response = yield call(getUserExaminationPaper,payload)
      yield put({
        type: 'savePaper',
        payload: {
          paper: response.data,
        }
      })
      callback&&callback(response.data);
    },
    *getExaminationPaper({ payload, callback }, { call, put }) {
      const response = yield call(getExaminationPaper)
      console.log(response.data)
      const paper = response.data
      if(paper.examStartDate){
        const examStartAndEndTime = [
          moment(response.data.examStartDate, "YYYY-MM-DD HH:mm:ss"),
          moment(response.data.examEndDate, "YYYY-MM-DD HH:mm:ss")
        ]
        const announceAnswer = moment(response.data.announceAnswer, "YYYY-MM-DD HH:mm:ss")
        delete paper.examEndDate
        paper['examStartAndEndTime'] = examStartAndEndTime
        paper['announceAnswer'] = announceAnswer
      }
      
      yield put({
        type: 'savePaperTitle',
        payload: {
          paperTitle: paper,
        }
      })
      callback&&callback(paper)
    },
    *setExamPaper({ payload, callback }, { call, put, select }) {
      console.log(payload)
      if (payload.examStartAndEndTime) {
        const examStartDate = payload.examStartAndEndTime[0].format("YYYY-MM-DD HH:mm:ss");
        const examEndDate = payload.examStartAndEndTime[1].format("YYYY-MM-DD HH:mm:ss");
        const announceAnswer = payload.announceAnswer.format("YYYY-MM-DD HH:mm:ss");
        const examBackup = payload.examBackup;
        const totalScore = payload.totalScore;
        const paperTitle = yield select(state => state.paper.paperTitle);
        const paper = {
          "id": paperTitle.id,
          "examStartDate": examStartDate,
          "examEndDate": examEndDate,
          "announceAnswer": announceAnswer,
          "examBackup": examBackup,
          "title": '',
          "totalScore": totalScore,
        }
        const response = yield call(setExamPaper, paper)
        yield put({
          type: 'getExaminationPaper',
        })
        callback&&callback(response.data)
      } else if (payload.title) {
        const title = payload.title;
        const paperTitle = yield select(state => state.paper.paperTitle);
        const paper = {
          "id": paperTitle.id,
          "title": title,
        }
        const response = yield call(setExamPaper, paper)
        yield put({
          type: 'getExaminationPaper',
        })
        callback(response.data)
      }
    },
    *getExaminationColumn({ payload, callback }, { call, put }) {
      const response = yield call(getExaminationColumn, payload)
      yield put({
        type: 'saveExaminationColumn',
        payload: {
          columnList: response.data,
        }
      })
      callback && callback(response.data)
    },
    *createExamColumn({ payload, callback }, { call, put, select }) {
      const response = yield call(createExamColumn, payload)
      if(response){
        yield put({
        type: 'getExaminationColumn',
        payload: {
          paperId: payload.paperId,
        },
        callback:(res)=>{
          callback&&callback(res)
        }
        })
      }
      
    },
    *createExamQuestion({ payload, callback }, { call, put, select }) {
      const response = yield call(createExamQuestion, payload)
      const column = yield select(state => state.paper.columnList)

      yield put({
        type: 'getExaminationQuestionByColumn',
        payload: {
          columnIds: column,
        }
      })
    },
    *setExamColumn({ payload, callback }, { call, put }) {
      const response = yield call(setExamColumn, payload)
      yield put({
        type: 'getExaminationColumn',
        payload: {
          paperId: payload.paperId,
        }
      })
      callback(response.data)
    },
    *getExaminationQuestionByColumn({ payload, callback }, { call, put }) {
      const columns = payload.columnIds
      const questionLists = [];
      for (let i = 0; i < columns.length; i++) {
        const column = {
          columnId: columns[i].id
        }
        const response = yield call(getExaminationQuestion, column)
        const list = [...response.data, '']
        console.log(list)
        questionLists.push(list);
      }
      yield put({
        type: 'saveExaminationQuestion',
        payload: {
          questionList: questionLists,
        }
      })
    },
    *setExamQuestion({ payload, callback }, { call, put, select }) {
      const response = yield call(setExamQuestion, payload)
      const column = yield select(state => state.paper.columnList)
      yield put({
        type: 'getExaminationQuestionByColumn',
        payload: {
          columnIds: column,
        }
      })
      callback(response.data)
    },
    *deleteExamColumn({ payload, callback }, { call, put, select }) {
      const response = yield call(deleteExamColumn, payload)
      yield put({
        type: 'getExaminationColumn',
        payload: {
          paperId: payload.paperId,
        }
      })
    },
    *deleteExamQuestion({ payload, callback }, { call, put, select }) {
      const response = yield call(deleteExamQuestion, payload)
      const column = yield select(state => state.paper.columnList)
      yield put({
        type: 'getExaminationQuestionByColumn',
        payload: {
          columnIds: column,
        }
      })
    },
    *setScore({ payload, callback }, { call, put, select }) {
      const response = yield call(setScore, payload)
      callback&&callback(response.data)
    },
    *getScore({ payload, callback }, { call, put, select }) {
      const response = yield call(getScore, payload)
      callback&&callback(response.data)
    }
  },

  reducers: {
    saveStepFormData(state, { payload }) {
      return {
        ...payload,
      };
    },
    savePaperTitle(state, { payload }) {
      return {
        ...state,
        paperTitle: payload.paperTitle,
      }
    },
    savePaper(state, { payload }){
      return {
        ...state,
        paper: payload.paper,
      }
    },
    saveExaminationColumn(state, { payload }) {
      return {
        ...state,
        columnList: payload.columnList,
      }
    },
    saveExaminationQuestion(state, { payload }) {
      return {
        ...state,
        questionList: payload.questionList,
      }
    },
  },
};